/*
 * @Author: 许警 11378748333@
 * @Date: 2024-05-31 14:25:23
 * @LastEditors: 许警 11378748333@
 * @LastEditTime: 2024-07-09 11:08:35
 * @FilePath: /hsxlsystem/src/main.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import components from '@/components/index'

// const app = createApp(App)
// 引入ant，修改主题色的时候,要修改为less
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.less'

import '../src/assets/css/global.less'

// 按需引入还是报错，不管咋
// const plugins = [Button]
// plugins.forEach(p => app.use(p))

// 引入路由拦截
import '@/router/permission'

// 引入自定义指令
import permission from './directive/permission/index'

// 图片懒加载
import lazyPlugin from 'vue3-lazy'

// 全局公共js
// import { deleteObjBlank } from '../src/assets/js/common.js'
// app.config.globalProperties.$deleteObjBlank = deleteObjBlank
// console.log(app,'app');

// 全局组件注册
const app = createApp(App)
  .use(store)
  .use(router)
  .use(Antd)
  .use(lazyPlugin, {
    loading: require('@/assets/img/default.png'), // 图片加载时默认图片
    error: require('@/assets/img/error.png') // 图片加载失败时默认图片
  })
  .use(permission)
app.directive('preventReClick', {
  inserted(el, binding) {
    console.log('binding-7', binding)

    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true

        setTimeout(() => {
          el.disabled = false
        }, binding.value || 4000)
      }
    })
  }
})
app.directive('has', {
  mounted(el, binding) {
    let flag = false

    let routers = JSON.parse(localStorage.getItem('routers'))
    const { value } = binding
    let key = value.b == 1 ? 'accessPermissions' : 'operationPermissions'
    function filterRoutes(routes, key, ke) {
      let arr = []
      routes.map(r => {
        if (r.children) {
          r.children = filterRoutes(r.children, key, ke)
        }
        if (r.name == key) {
          flag = r[ke]
          arr.push(flag)
          return
        }
      })

      return arr
    }
    let res = filterRoutes(routers, value.a.meta.title, key)
    // console.log(flag);
    if (flag == 1) {
      return true
    } else {
      return true
      // el.parentNode && el.parentNode.removeChild(el);
    }
  }
})
components.forEach(c => {
  app.component(c[0], c[1])
})

router.isReady().then(() => app.mount('#app'))
// router.onError((err) => console.error(err));
